/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-6-11
 */
package com.autotag;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import fanglu.util.FileDealer;

public class AllTagSelector {
	public static void main(String args[]){
		
		ArrayList<String> domainNameList = new ArrayList<String>();
		domainNameList.add("Value manipulation");
		domainNameList.add("Utility");
		domainNameList.add("Content");
		domainNameList.add("Business");
		domainNameList.add("Consumer");
		domainNameList.add("Miscellaneous");
		domainNameList.add("Science");
		HashMap<String, Integer> UUIDMapLine = new HashMap<String, Integer>();
		HashMap<Integer, String> lineMapUUID = new HashMap<Integer, String>();
		DomainTagGenerator.initLineMapUUID("train/linemapuuid.txt", UUIDMapLine, lineMapUUID);
		HashMap<String, HashSet<String>> UUIDMapDomainSet = getUUIDMapDomainSet("C:/Users/ftc/Desktop/ICSOC/final_data", 
				domainNameList, lineMapUUID);
		HashMap<String, HashSet<String>> UUIDMapEnvSet = getUUIDMapTagSet("C:/Users/ftc/Desktop/ICSOC/WSDL_FILE/result/env_tag.txt");
		HashMap<String, HashSet<String>> UUIDMapOwnerSet = getUUIDMapTagSet("C:/Users/ftc/Desktop/ICSOC/WSDL_FILE/result/owner_tag.txt");
		HashMap<String, HashSet<String>> UUIDMapFuncSet = getUUIDMapTagSet("C:/Users/ftc/Desktop/ICSOC/WSDL_FILE/result/select_tag.txt");
		
		writeTag("result/tag.txt", UUIDMapDomainSet, UUIDMapEnvSet, UUIDMapOwnerSet, UUIDMapFuncSet);
	}
	
	public static void writeTag(String fileName, 
			HashMap<String, HashSet<String>> UUIDMapDomainSet, 
			HashMap<String, HashSet<String>> UUIDMapEnvSet, 
			HashMap<String, HashSet<String>> UUIDMapOwnerSet,
			HashMap<String, HashSet<String>> UUIDMapFuncSet){
		File file = new File(fileName);
		try {
			FileWriter fw = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
			
			Iterator<String> itor_uuid = UUIDMapOwnerSet.keySet().iterator();
			while (itor_uuid.hasNext()){
				String thisUUID = itor_uuid.next();
				bw.write("#UUID:" + thisUUID + "\n");
				//Onwer Tag
				HashSet<String> thisOwnerSet = UUIDMapOwnerSet.get(thisUUID);
				bw.write("Owner Tag:");
				if (thisOwnerSet != null){
					boolean isFirst = true;
					for (String ownerTag : thisOwnerSet) {
						if (isFirst != true){
							bw.write(",");
						}else{
							isFirst = false;
						}
						bw.write(ownerTag);
					}
				}
				bw.write("\n");
				
				//Environment Tag
				HashSet<String> thisEnvSet = UUIDMapEnvSet.get(thisUUID);
				bw.write("Environment Tag:");
				if (thisEnvSet != null){
					boolean isFirst = true;
					for (String envTag : thisEnvSet) {
						if (isFirst != true){
							bw.write(",");
						}else{
							isFirst = false;
						}
						bw.write(envTag);
					}
				}
				bw.write("\n");
				
				
				//Functionality Tag
				HashSet<String> thisFuncSet = UUIDMapFuncSet.get(thisUUID);
				bw.write("Functionality Tag:");
				if (thisFuncSet != null){
					boolean isFirst = true;
					for (String funcTag : thisFuncSet) {
						if (isFirst != true){
							bw.write(",");
						}else{
							isFirst = false;
						}
						bw.write(funcTag);
					}
				}
				bw.write("\n");
				
				//Domain Tag
				HashSet<String> thisDomainSet = UUIDMapDomainSet.get(thisUUID);
				bw.write("Domain Tag:");
				if (thisDomainSet != null){
					boolean isFirst = true;
					for (String domainTag : thisDomainSet) {
						if (isFirst != true){
							bw.write(",");
						}else{
							isFirst = false;
						}
						bw.write(domainTag);
					}
				}
				bw.write("\n");
			}
			
			bw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static HashMap<String, HashSet<String>> getUUIDMapDomainSet(String dirFileName, 
			ArrayList<String> domainNameList, 
			HashMap<Integer, String> lineMapUUID){
		HashMap<String, HashSet<String>> result = new HashMap<String, HashSet<String>>();
		
		for (int i=0; i<domainNameList.size(); ++i){
			
			try {
				String content = FileDealer.getFileContent(dirFileName + "/" + "o" + (i+1));
				StringReader sr = new StringReader(content);
				BufferedReader br = new BufferedReader(sr);
				String oneLine = br.readLine();
				int index = 0;
				while (oneLine != null){
					oneLine = oneLine.trim();
					if (oneLine.equals("-1")){
						
					}else{
						String thisUUID = lineMapUUID.get(index);
						if (thisUUID != null){
							HashSet<String> thisSet = result.get(thisUUID);
							if (thisSet == null){
								thisSet = new HashSet<String>();
								result.put(thisUUID, thisSet);
							}
							thisSet.add(domainNameList.get(i));
						}
					}
										
					oneLine = br.readLine();
					++ index;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return result;
	}
	public static HashMap<String, HashSet<String>> getUUIDMapTagSet(String fileName){
		HashMap<String, HashSet<String>> result = new HashMap<String, HashSet<String>>();
		try {
			String content = FileDealer.getFileContent(fileName);
			StringReader sr = new StringReader(content);
			BufferedReader br = new BufferedReader(sr);
			String oneLine = br.readLine();
			HashSet<String> tagSet = new HashSet<String>();
			String thisUUID = "";
			while (oneLine != null){
				oneLine = oneLine.trim();
				if (oneLine.startsWith("#")){
					thisUUID = new String(oneLine.substring(1));
					if (thisUUID.endsWith(".html")){
						thisUUID = thisUUID.substring(0, thisUUID.length() - 5);
					}
					tagSet = new HashSet<String>();
					result.put(thisUUID, tagSet);
				}else if (oneLine.length() > 0){
					tagSet.add(oneLine);
				}
				oneLine = br.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
}
